﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.UserControls;

namespace Labthink.PCSoft.UniversalHost4.Instrument.Forms
{
    public partial class FrmParamForTrace
    {
        public List<ParamTraceReason> Ptrs { get; set; }

        public FrmParamForTrace()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void FrmParamForTrace_OnLoaded(object sender, RoutedEventArgs e)
        {
         
            CanvasReason.Children.Clear();
            var leftTb = 15.0;
            var topTxt = 10.0;
            var step = 50.0;

            foreach (var ptr in Ptrs)
            {
              
                var txt = new UcTextBox { Width =760, MaxLength = 50,Label = ptr.Name};
                CanvasReason.Children.Add(txt);
                CanvasReason.RegisterName("Txt" + ptr.DataNo + ptr.CupNo, txt);
                txt.SetValue(Canvas.TopProperty, topTxt);
                txt.SetValue(Canvas.LeftProperty, leftTb);
                
                topTxt += step;
                if (topTxt > CanvasReason.ActualHeight)
                {
                    CanvasReason.SetValue(HeightProperty, topTxt + step * 2);
                }
            }
        }

        private void ButtonOk_OnClick(object sender, RoutedEventArgs e)
        {
            var all = true;
            foreach (var ptr in Ptrs)
            {
                if (CanvasReason.FindName("Txt" + ptr.DataNo + ptr.CupNo) is UcTextBox txt)
                {
                    if (string.IsNullOrEmpty(txt.Value) || txt.Value.Trim() == "")
                    {
                        txt.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Red));
                        all = false;
                    }
                    else
                    {
                        ptr.Reason = txt.Value;
                    }
                }
            }
            if (all)
            {
                DialogResult = true;
                Close();
            }
        }
    }
}
